package com.wc.AlgoOJ_train.train2_DP训练.c8.DP0503_数数2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/18 13:24
 * @description http://43.138.190.70:8888/p/DP0503
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static long l, r;
    static int N = 18;
    // f[i][j] i 位最高位为j满足条件的
    static long[][] f = new long[N][N * 9 + 1];
    // f1[i][j] 表示i个数合成j的有多少种
    static long[][] f1 = new long[N][N * 9 + 1];

    static int[] nums = new int[N];
    static boolean[] isNotPrimes = new boolean[N * 9 + 1];
    static int[] primes = new int[N * 9 + 1];
    static int idx = 0;

    static void ola(int x) {
        isNotPrimes[0] = isNotPrimes[1] = true;
        for (int i = 2; i <= x; i++) {
            if (!isNotPrimes[i]) {
                primes[++idx] = i;
            }
            for (int j = 1; i * primes[j] <= x; j++) {
                isNotPrimes[i * primes[j]] = true;
                if (i % primes[j] == 0) break;
            }
        }
    }

    static void init() {
        ola(N * 9);
        // 组合数
        for (int i = 0; i <= 9; i++) {
            // 当n是个位数的时候，需要f1[0]来判定
            f1[0][i] = 1;
            f1[1][i] = 1;
        }
        for (int i = 2; i < N; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= (i - 1) * 9; k++) {
                    f1[i][j + k] += f1[i - 1][k];
                }
            }
        }
        // 开始整f
        for (int i = 0; i <= 9; i++) {
            if (!isNotPrimes[i]) {
                f[1][i] = f1[1][i];
            }
        }
        for (int i = 2; i < N; i++) {
            for (int j = 0; j <= 9; j++) {
                for (int k = 0; k <= (i - 1) * 9; k++) {
                    if (!isNotPrimes[k + j]) {
                        f[i][j] += f1[i - 1][k];
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        init();
        l = sc.nextLong();
        r = sc.nextLong();
        out.println(dp(r) - dp(l - 1));
        out.flush();
    }

    static long dp(long n) {
        if (n == 0) return 0;
        int len = 0;
        while (n > 0) {
            nums[len++] = (int) (n % 10);
            n /= 10;
        }
        long res = 0;
        int last = 0;
        for (int i = len - 1; i >= 0; i--) {
            int x = nums[i];
            // 左分支
            for (int j = i == len - 1 ? 1 : 0; j < x; j++) {
                for (int k = 0; k <= i * 9; k++) {
                    if (!isNotPrimes[k + j + last]) {
                        // 0 ~ i - 1
                        res += f1[i][k];
                    }
                }
            }

            // 右分支
            last += x;

            if (i == 0 && !isNotPrimes[last]) res++;
        }

        // 前导0的数
        for (int i = 1; i < len; i++) {
            for (int j = 1; j <= 9; j++) {
                res += f[i][j];
            }
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
